using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.Security.Permissions;

// This example shows how to create your own column style that
// hosts a control, in this case, a DateTimePicker.
public class DataGridButtonColumnStyle : DataGridColumnStyle
{
    Button button 	=  new Button();    

    // The isEditing field tracks whether or not the user is
    // editing data with the hosted control.
    private bool isEditing;

    public DataGridButtonColumnStyle() : base(){
        button.Text 	= "...";    	
    }

    protected override void Abort(int rowNum){
                
        Invalidate();
    }

    protected override Size GetPreferredSize(
        Graphics g,
        object value)
    {
        return new Size(100, button.PreferredSize.Height + 4);
    }

    protected override int GetMinimumHeight()
    {
        return button.PreferredSize.Height + 4;
    }

    protected override int GetPreferredHeight(Graphics g,
        object value)
    {
        return button.PreferredSize.Height + 4;
    }

    protected override void Paint(Graphics g,Rectangle bounds,int rowNum){
    	CurrencyManager source;
        Paint(g, bounds, source, rowNum, false);
    }

    protected override void Paint(
        Graphics g,
        Rectangle bounds,
        CurrencyManager source,
        int rowNum,
        bool alignToRight)    {
        Paint(
            g, bounds,
            source,
            rowNum,
            Brushes.Red,
            Brushes.Blue,
            alignToRight);
    }

    protected override void Paint(
        Graphics g,
        Rectangle bounds,
        CurrencyManager source,
        int rowNum,
        Brush backBrush,
        Brush foreBrush,
        bool alignToRight)    {
        DateTime date = (DateTime)
            GetColumnValueAtRow(source, rowNum);
        Rectangle rect = bounds;
        g.FillRectangle(backBrush, rect);
        rect.Offset(0, 2);
        rect.Height -= 2;
        g.DrawString(date.ToString("d"),
            this.DataGridTableStyle.DataGrid.Font,
            foreBrush, rect);
    }

    protected override void SetDataGridInColumn(DataGrid value){
        base.SetDataGridInColumn(value);
        if (button.Parent != null){
            button.Parent.Controls.Remove
                (button);
        }
        if (value != null){
            value.Controls.Add(button);
        }
    }
}